// @ts-nocheck

// 获取url参数
export function getParamsFromURL(url: any) {
  if ("undefined" == url) {
    return {};
  }
  const paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
  let paraObj: any = {};
  for (let i = 0; i < paraString.length; i++) {
    let j = paraString[i];
    paraObj[j.substring(0, j.indexOf("="))] = j.substring(
      j.indexOf("=") + 1,
      j.length
    );
  }
  return paraObj;
}

// 日期格式化
export const format = function (fmt: string) {
  if (!fmt) {
    return "";
  }
  var _that = new Date(fmt);
  var o = {
    "M+": _that.getMonth() + 1,
    "d+": _that.getDate(),
    "h+": _that.getHours(),
    "m+": _that.getMinutes(),
    "s+": _that.getSeconds(),
    "q+": Math.floor((_that.getMonth() + 3) / 3),
    S: _that.getMilliseconds(),
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (_that.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
};

// url参数转对象
export const getParamsFromURLSearch = (): any => {
  var url = window.location.search;
  if (!url) {
    return {};
  }
  let paraString: string[] = url
    .substring(url.indexOf("?") + 1, url.length)
    .split("&");
  let paraObj: any = {};
  for (var i = 0; i < paraString.length; i++) {
    var j = paraString[i];
    paraObj[j.substring(0, j.indexOf("="))] = j.substring(
      j.indexOf("=") + 1,
      j.length
    );
  }
  return paraObj;
};

//json转url参数
export const convertJsonToUrl = function (data) {
  var _result = [];
  for (var key in data) {
    var value = data[key];
    if (value.constructor == Array) {
      value.forEach(function (_value) {
        _result.push(key + "=" + _value);
      });
    } else {
      _result.push(key + "=" + value);
    }
  }
  return _result.join("&");
};

// 初始化处理dyeing
export function processParams() { 
  let _params = getParamsFromURL(location.href) || {};
  _params[Dyeing] && setDyeing(_params[Dyeing]);
  _params[AppId] && setCookie(AppId, _params[AppId]);
}